package com.bike.service.impl;

import java.math.BigInteger;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.SysRole;
import com.bike.mapper.SysRoleMapper;
import com.bike.util.PageData;
import com.bike.util.RightsHelper;
import com.bike.util.Tools;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class SysRoleServiceImpl extends BaseServcieImpl  {

	@Autowired
	SysRoleMapper sysRoleMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, SysRole record) {
		if(pd.getLong("roleId")>0){
			 //编辑
			sysRoleMapper.updateByPrimaryKeySelective(record);
		}else{
			sysRoleMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteSysRoleById(long roleId) {
		return sysRoleMapper.deleteByPrimaryKey(roleId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectSysRoleList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<SysRole> records= sysRoleMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<SysRole>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<SysRole> listAll(PageData pd) {
		return  sysRoleMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public SysRole selectBySysRoleId(Long roleId) {
		return sysRoleMapper.selectByPrimaryKey(roleId);
	}
 
	public void bingRight(PageData pd,int type) {
		long roleId=pd.getLong("roleId");
		Long menuId=pd.getLong("menuId");
		
		SysRole role=sysRoleMapper.selectByPrimaryKey(roleId);
		if(role!=null){
			String menuRight=role.getMenuRights();
			if(StringUtils.isEmpty(menuRight)){
				menuRight="0";
			}
			if(new BigInteger(menuRight).testBit(menuId.intValue())){//清除某个权限
				menuRight=new BigInteger(menuRight).clearBit(menuId.intValue()).toString();
			}else{//赋予某个权限
				menuRight=new BigInteger(menuRight).setBit(menuId.intValue()).toString();
			}
			role.setMenuRights(menuRight);
			sysRoleMapper.updateByPrimaryKeySelective(role);
		}
	}

	public void saveRights(PageData pd) {
			 
			//String type = pd.getString("TYPE");
		SysRole record=new SysRole();
		String type = "menu_qx";
		record.setRoleId(pd.getLong("roleId"));
		if(!"".equals(pd.getString("MENUIDS").trim())){
			String menuIds = pd.getString("MENUIDS");
			BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
			if("add_qx".equals(type)){
				pd.put("ADD_QX", rights.toString());
			}else if("delete_qx".equals(type)){
				pd.put("DEL_QX", rights.toString());
			}else if("update_qx".equals(type)){
				pd.put("EDIT_QX", rights.toString());
			}else if("select_qx".equals(type)){
				pd.put("CHA_QX", rights.toString());
			}else if("menu_qx".equals(type)){
				pd.put("menuRights", rights.toString());
				record.setMenuRights(rights.toString());
			}
		}else{
			String rights = "";
			if("add_qx".equals(type)){
				pd.put("ADD_QX", rights.toString());
			}else if("delete_qx".equals(type)){
				pd.put("DEL_QX", rights.toString());
			}else if("update_qx".equals(type)){
				pd.put("EDIT_QX", rights.toString());
			}else if("select_qx".equals(type)){
				pd.put("CHA_QX", rights.toString());
			}else if("menu_qx".equals(type)){
				pd.put("menuRights", rights.toString());
				record.setMenuRights(rights.toString());
			}
		}
		 saveOrUpdate(pd,record);
	}
		 
	  
}

